package com.example.twoscroolview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class CustomSRL2 extends SwipeRefreshLayout {

    //外部拦截成员变量
    private float startX;
    private float startY;
    //ViewPager是否滚动
    boolean mIsVpMove = false;
    //触发移动事件的最小距离,如果小于这个距离就不触发移动控件，如Viewpager就是用这个距离来判断用户是否翻页
    private int mTouchSlop;

    public CustomSRL2(Context context) {
        this(context, null);
    }

    public CustomSRL2(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    //外部拦截
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = ev.getX();
                startY = ev.getY();
                mIsVpMove = false;
                break;
            case MotionEvent.ACTION_MOVE:
                //若此时ViewPager还在滑动，则返回false,不拦截
                if (mIsVpMove) {
                    return false;
                }
                float x = ev.getX();
                float y = ev.getY();
                float deltaX = Math.abs(x - startX);
                float deltaY = Math.abs(y - startY);
                if (deltaX > mTouchSlop && deltaX > deltaY) {
                    mIsVpMove = true;
                    return false;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mIsVpMove = false;
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
}